//
//  CZNewFeatureViewController.swift
//  iOSWeibo10
//
//  Created by itcast on 16/5/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let ItemCount = 4

class CZNewFeatureViewController: UICollectionViewController {
    
    // MARK: - 属性
    /// layout
    private let layout = UICollectionViewFlowLayout()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init() {
        super.init(collectionViewLayout: layout)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        prepareCollectionView()
    }
    
    private func prepareCollectionView() {
        // 注册cell
        self.collectionView!.registerClass(CZNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        // 设置layout参数
        // 让cell和屏幕一样大
        layout.itemSize = UIScreen.mainScreen().bounds.size
        // 水平滚动
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        // 间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        // 取消弹簧效果
        collectionView?.bounces = false
        // 不要水平滚动条
//        collectionView?.showsHorizontalScrollIndicator = false
        // 分页
        collectionView?.pagingEnabled = true
    }

}

extension CZNewFeatureViewController {
    // 某一组显示几个cell
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ItemCount
    }
    
    // 每个cell张什么样
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // 直接取cell
        // dequeueReusableCellWithReuseIdentifier: 取cell会先去缓存池中取cell,缓存池中取不到会使用注册的cell来创建
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CZNewFeatureCell
        
        cell.backgroundColor = UIColor.brownColor()
        
        // 设置cell显示的index
        cell.index = indexPath.item
        
        if indexPath.item == ItemCount - 1 {
            cell.shareButton.hidden = false
            cell.startButton.hidden = false
        } else {
            cell.shareButton.hidden = true
            cell.startButton.hidden = true
        }
        
        return cell
    }
}
